package cn.z2huo.algorithm.sort.swapSort;

import java.util.Arrays;

/**
 * 交换排序，时间复杂度为O(n^2)
 * 作者： 张一卓
 * 时间： 2020/1/8 17:42
 */
public class SwapSorting {
    public int[] sort(int[] array) {
        if (array == null) {
            return null;
        }
        int moveTimes = 0;
        int compareTimes = 0;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[i]) {
                    int temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                    moveTimes++;
                }
                compareTimes++;
            }
        }
        System.out.println(Arrays.toString(array));
        System.out.println("移动次数：" + moveTimes);
        System.out.println("比较次数：" + compareTimes);
        return array;
    }
}
